package com.egreen.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.egreen.common.Result;
import com.egreen.common.pojo.DTO.order.AddRecyclableNameDTO;
import com.egreen.common.pojo.entity.order.RecyclableName;
import com.egreen.common.pojo.entity.order.RecyclableType;
import com.egreen.order.service.IRecyclableNameService;
import com.egreen.order.service.IRecyclableTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author cloudJiang
 * @date 2022/7/22 16:22
 */
@RestController
@RequestMapping("/recyclable")
public class RecyclableController {

    @Autowired
    IRecyclableNameService recyclableNameService;
    @Autowired
    IRecyclableTypeService recyclableTypeService;

    @GetMapping("/addRecyclableType")
    Result addRecyclableType(@RequestParam("typeName") String typeName) {
        RecyclableType recyclableType = new RecyclableType(null, typeName);
        System.out.println(recyclableType);
        recyclableTypeService.save(recyclableType);
        return Result.OK();
    }

    @PostMapping("/addRecyclableName")
    Result addRecyclableName(@RequestBody AddRecyclableNameDTO addRecyclableNameDTO) {
        recyclableNameService.save(new RecyclableName(null, addRecyclableNameDTO.getTypeId(),
                addRecyclableNameDTO.getName(), addRecyclableNameDTO.getDescription(),
                addRecyclableNameDTO.getPrice()));

        return Result.OK();
    }

    @GetMapping("/getRecyclableType")
    Result getRecyclableType() {
        return Result.OK(recyclableTypeService.list());
    }

    @GetMapping("/getRecyclableByType")
    Result getRecyclableByType(@RequestParam("typeId") Integer typeId) {
        //拼接查询条件
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("id", "type_id","name", "description", "price");
        wrapper.eq(typeId != null, "type_id", typeId);
        return Result.OK(recyclableNameService.list(wrapper));
    }
}
